# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=cmake
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         $( [[ ${CARCH} == i686 ]] || echo \
           "${MINGW_PACKAGE_PREFIX}-ccmake" \
           "${MINGW_PACKAGE_PREFIX}-${_realname}-cmcldeps" \
           "${MINGW_PACKAGE_PREFIX}-${_realname}-gui" \
           "${MINGW_PACKAGE_PREFIX}-${_realname}-docs"))
pkgver=4.0.2
_rc=""
_pkgver=${pkgver}${_rc}
pkgrel=1
pkgdesc="A cross-platform open-source make system"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.cmake.org/"
msys2_repository_url="https://gitlab.kitware.com/cmake/cmake"
msys2_references=(
  'archlinux: cmake'
  "cpe: cpe:2.3:a:cmake_project:cmake"
)
license=("spdx:MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake-bootstrap"
             "${MINGW_PACKAGE_PREFIX}-bzip2"
             "${MINGW_PACKAGE_PREFIX}-xz"
             "${MINGW_PACKAGE_PREFIX}-zstd"
             $([[ ${CARCH} == i686 ]] || echo \
               "${MINGW_PACKAGE_PREFIX}-emacs" \
               "${MINGW_PACKAGE_PREFIX}-fc" \
               "${MINGW_PACKAGE_PREFIX}-ncurses" \
               "${MINGW_PACKAGE_PREFIX}-python" \
               "${MINGW_PACKAGE_PREFIX}-python-sphinx" \
               "${MINGW_PACKAGE_PREFIX}-qt6-base" \
               "${MINGW_PACKAGE_PREFIX}-qt6-tools"))
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-cppdap"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-jsoncpp"
         "${MINGW_PACKAGE_PREFIX}-libarchive"
         "${MINGW_PACKAGE_PREFIX}-libuv"
         "${MINGW_PACKAGE_PREFIX}-ninja"
         "${MINGW_PACKAGE_PREFIX}-pkgconf"
         "${MINGW_PACKAGE_PREFIX}-rhash"
         "${MINGW_PACKAGE_PREFIX}-zlib")
source=("https://github.com/Kitware/CMake/releases/download/v${_pkgver}/${_realname}-${_pkgver}.tar.gz"
        "0001-Disable-response-files-for-MSYS-Generator.patch"
        "0002-Do-not-install-Qt-bundle-in-cmake-gui.patch"
        "0003-fix-find-python-on-mingw-aarch64.patch"
        "0005-Default-to-ninja-generator.patch")
sha256sums=('1c3a82c8ca7cf12e0b17178f9d0c32f7ac773bd5651a98fcfd80fbf4977f8d48'
            '25793edcbac05bb6d17fa9947b52ace4a6b5ccccf7758e22ae9ae022ed089061'
            'f6cf6a6f2729db2b9427679acd09520af2cd79fc26900b19a49cead05a55cd1a'
            '557b5cbc05d4d50b3a67a7892391fcaa5cd95c492cdb4338d86305d1f4a3b88a'
            '426818278090704d2a12f62ef3dfd94c47b11fa2784bb842989b7f6a09ee7aa2')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd ${_realname}-${_pkgver}
  apply_patch_with_msg \
    0001-Disable-response-files-for-MSYS-Generator.patch \
    0002-Do-not-install-Qt-bundle-in-cmake-gui.patch \
    0003-fix-find-python-on-mingw-aarch64.patch

  # We want cmake to default to something useful and not MSVC
  apply_patch_with_msg \
    0005-Default-to-ninja-generator.patch

  find . -name "*.orig" -exec rm -f {} \;
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${CARCH} != i686 ]]; then
    export PATH=${MINGW_PREFIX}/share/qt6/bin:$PATH
    _extra_config+=(
      "-DSPHINX_HTML=ON"
      "-DSPHINX_MAN=ON"
      "-DSPHINX_QTHELP=ON"
      "-DBUILD_QtDialog=ON"
      "-DBUILD_CursesDialog=ON"
      "-DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -GNinja \
    "${_extra_config[@]}" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMake_INSTALL_COMPONENTS=ON \
    -DCMAKE_DATA_DIR=share/cmake \
    -DCMAKE_DOC_DIR=share/doc/cmake \
    -DCMAKE_MAN_DIR=share/man \
    -DCMAKE_INFO_DIR=share/info \
    -DCMAKE_USE_SYSTEM_LIBRARIES=ON \
    -DBUILD_TESTING=OFF \
    ../${_realname}-${_pkgver}
  ${MINGW_PREFIX}/bin/cmake.exe --build .
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  ${MINGW_PREFIX}/bin/cmake -DBUILD_TESTING=ON ../${_realname}-${_pkgver}
  ${MINGW_PREFIX}/bin/cmake --build .
  ${MINGW_PREFIX}/bin/ctest.exe -j$(($(nproc)+1)) || msg2 "Tests failed"
}

package_cmake() {
  pkgdesc+=" (mingw-w64)"
  if [[ ${CARCH} != i686 ]]; then
    optdepends=("${MINGW_PACKAGE_PREFIX}-emacs: for cmake emacs mode")
  fi

  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install . --component cmake
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install . --component cpack
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install . --component ctest
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install . --component sphinx-man
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install . --component Unspecified

  if [[ ${CARCH} != i686 ]]; then
    install -d "${pkgdir}${MINGW_PREFIX}"/share/emacs/site-lisp/
    ${MINGW_PREFIX}/bin/emacs -batch -f batch-byte-compile \
      "${pkgdir}${MINGW_PREFIX}"/share/emacs/site-lisp/cmake-mode.el
  fi
}

package_cmake-cmcldeps() {
  pkgdesc="CMake wrapper for cl to extract dependencies (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-cmake")

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install . --component cmcldeps
}

package_cmake-gui() {
  pkgdesc+=" (Qt GUI) (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-cmake"
           "${MINGW_PACKAGE_PREFIX}-qt6-base")

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install . --component cmake-gui
}

package_ccmake() {
  pkgdesc+=" (curses GUI) (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-cmake")

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install . --component ccmake
}

package_cmake-docs() {
  pkgdesc+=" (documentation) (mingw-w64)"
  depends=()
  conflicts=("${MINGW_PACKAGE_PREFIX}-cmake-doc-qt")
  replaces=("${MINGW_PACKAGE_PREFIX}-cmake-doc-qt")
  provides=("${MINGW_PACKAGE_PREFIX}-cmake-doc-qt")

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install . --component sphinx-html
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install . --component sphinx-qthelp
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
